package leetcode_solution;

import java.util.HashSet;
import java.util.Set;

/**
 * 2336. 无限集中的最小数字
 *
 * @author nagiumi
 * @date 2023/11/29
 */
public class Solution2336 {

    class SmallestInfiniteSet {

        private int smallestInt;
        private int setBeginInt;
        private Set<Integer> backSet;

        public SmallestInfiniteSet() {
            smallestInt = 1;
            setBeginInt = 1;
            backSet = new HashSet<>();
        }

        public int popSmallest() {
            int out = smallestInt;
            if (backSet.contains(smallestInt)) {
                backSet.remove(smallestInt);
                smallestInt = setBeginInt;
                for (Integer i : backSet) {
                    if (i < smallestInt) {
                        smallestInt = i;
                    }
                }
            } else {
                setBeginInt++;
                smallestInt = setBeginInt;
            }
            return out;
        }

        public void addBack(int num) {
            if (num >= setBeginInt) {
                return;
            }
            if (num < smallestInt) {
                smallestInt = num;
            }
            backSet.add(num);
        }
    }

/**
 * Your SmallestInfiniteSet object will be instantiated and called as such:
 * SmallestInfiniteSet obj = new SmallestInfiniteSet();
 * int param_1 = obj.popSmallest();
 * obj.addBack(num);
 */
}
